<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="车辆名称" prop="carName">
        <el-input
            v-model="queryParams.carName"
            clearable
            maxlength="50"
            placeholder="请输入车辆名称"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆品牌" prop="brand">
        <el-input
            v-model="queryParams.brand"
            clearable
            maxlength="50"
            placeholder="请输入车辆品牌"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车架号" prop="carVin">
        <el-input
            v-model="queryParams.carVin"
            clearable
            maxlength="50"
            placeholder="请输入车架号"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['train:info:add']"
            icon="Plus"
            plain
            type="primary"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['train:info:remove']"
            :disabled="multiple"
            icon="Delete"
            plain
            type="danger"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="ID" prop="id" width="100"/>
      <el-table-column align="center" label="用户名称" prop="nickName" width="150"/>
      <el-table-column align="center" label="车辆名称" prop="carName" width="150"/>
      <el-table-column align="center" label="车辆品牌" prop="brand" width="150"/>
      <el-table-column align="center" label="车辆图片" prop="carPic" width="110">
        <template #default="scope">
          <image-preview :src="scope.row.carPic"
                         fit="cover"
                         preview-teleported="true" style="width:50px;object-fit: cover; height:50px;"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="车架号" prop="carVin">
        <template #default="scope">
          <el-tag style="font: 1em sans-serif;" type="success">{{ scope.row.carVin }}</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="长宽高" align="center" prop="carSize" width="150">
        <template #default="scope">
          <span type="success" >{{scope.row.carSize}} cm</span>
        </template>
      </el-table-column> -->
      <el-table-column align="center" label="电池电量" prop="batteryPower" width="150">
        <template #default="scope">
          <span type="success">{{ scope.row.batteryPower }} Ah</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="续航里程" prop="enduranceMileage" width="110">
        <template #default="scope">
          <span type="success">{{ scope.row.enduranceMileage }} km</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template #default="scope">
          <el-button
              v-if="scope.row.userId != null"
              v-hasPermi="['train:info:edit']"
              icon="unlock"
              type="text"
              @click="unLockUser(scope.row)"
          >解绑
          </el-button>
          <el-button
              v-else
              v-hasPermi="['train:info:edit']"
              icon="Lock"
              type="text"
              @click="lockUser(scope.row)"
          >绑定
          </el-button>
<!--          <el-button-->
<!--              v-hasPermi="['train:info:edit']"-->
<!--              icon="open"-->
<!--              type="text"-->
<!--              @click="controllCar(scope.row)"-->
<!--          >控制-->
<!--          </el-button>-->
          <el-button
              v-hasPermi="['train:info:edit']"
              icon="view"
              type="text"
              @click="handleView(scope.row)"
          >详细
          </el-button>
          <el-button
              v-hasPermi="['train:info:edit']"
              icon="Edit"
              type="text"
              @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
              v-hasPermi="['train:info:remove']"
              icon="Delete"
              type="text"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

    <!-- 添加或修改车辆管理信息对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="40%">
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="车辆名称" prop="carName">
              <el-input
                  v-model="form.carName"
                  :disabled="disabledFlag"
                  maxlength="30"
                  placeholder="请输入车辆名称"
                  show-word-limit
                  style="width: 230px;"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆品牌" label-width="100px" prop="brand">
              <el-input
                  v-model="form.brand"
                  :disabled="disabledFlag"
                  maxlength="20"
                  placeholder="请输入车辆品牌"
                  show-word-limit
                  style="width: 230px;"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="车架号" prop="carVin">
              <el-input
                  v-model="form.carVin"
                  :disabled="disabledFlag"
                  maxlength="50"
                  placeholder="请输入车架号"
                  show-word-limit
                  style="width: 230px;"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="马力值" label-width="100px" prop="horsePower">
              <el-input
                  v-model="form.horsePower"
                  :disabled="disabledFlag"
                  oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="请输入马力值"
                  style="width: 230px;"
              >
                <template #append>
                  <span>ps</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="最大功率" prop="maxPower">
              <el-input
                  v-model="form.maxPower"
                  :disabled="disabledFlag"
                  oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="请输入最大功率"
                  style="width: 230px;"
              >
                <template #append>
                  <span>kW</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大扭矩" label-width="100px" prop="maxTorque">
              <el-input
                  v-model="form.maxTorque"
                  :disabled="disabledFlag"
                  oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="请输入最大扭矩"
                  style="width: 230px;"
              >
                <template #append>
                  <span>N·m</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="长宽高" prop="carSize">
              <el-input
                  v-model="form.carSize"
                  :disabled="disabledFlag"
                  maxlength="20"
                  placeholder="请输入长宽高"
                  show-word-limit
                  style="width: 230px;"
              >
                <template #append>
                  <span>cm</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="轴距" label-width="100px" prop="wheelBase">
              <el-input
                  v-model="form.wheelBase"
                  :disabled="disabledFlag"
                  oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="请输入轴距"
                  style="width: 230px;"
              >
                <template #append>
                  <span>mm</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="前轮距" prop="frontWheelBase">
              <el-input
                  v-model="form.frontWheelBase"
                  :disabled="disabledFlag"
                  oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="请输入前轮距"
                  style="width: 230px;"
              >
                <template #append>
                  <span>mm</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="后轮距" label-width="100px" prop="backWheelBase">
              <el-input
                  v-model="form.backWheelBase"
                  :disabled="disabledFlag"
                  oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="请输入后轴距"
                  style="width: 230px;"
              >
                <template #append>
                  <span>mm</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="续航里程" prop="enduranceMileage">
              <el-input
                  v-model="form.enduranceMileage"
                  :disabled="disabledFlag"
                  oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="请输入续航里程"
                  style="width: 230px;"
              >
                <template #append>
                  <span>km</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="百公里耗电" label-width="100px" prop="kmh">
              <el-input
                  v-model="form.kmh"
                  :disabled="disabledFlag"
                  oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="请输入百公里耗电"
                  style="width: 230px;"
              >
                <template #append>
                  <span>kWh</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电池编码" prop="batteryCode">
              <el-input v-model="form.batteryCode" :disabled="disabledFlag"
                        maxlength="50" placeholder="请输入电池编码" show-word-limit style="width: 230px;"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电池类型" label-width="100px" prop="batteryType">
              <el-input v-model="form.batteryType" :disabled="disabledFlag"
                        maxlength="50" placeholder="请输入电池类型" show-word-limit style="width: 230px;"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电池电量" prop="batteryPower">
              <el-input
                  v-model="form.batteryPower"
                  :disabled="disabledFlag"
                  oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="请输入电池电量"
                  style="width: 230px;"
              >
                <template #append>
                  <span>Ah</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电池容量" label-width="100px" prop="batteryCapacity">
              <el-input
                  v-model="form.batteryCapacity"
                  :disabled="disabledFlag"
                  oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="请输入电池容量"
                  style="width: 230px;"
              >
                <template #append>
                  <span>Ah</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工作温度" prop="workingTemperature">
              <el-input
                  v-model="form.workingTemperature"
                  :disabled="disabledFlag"
                  oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="请输入工作温度"
                  show-word-limit
                  style="width: 230px;"
              >
                <template #append>
                  <span>℃</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="NFC" label-width="100px" prop="nfc">
              <el-input
                  v-model="form.nfc"
                  :disabled="disabledFlag"
                  maxlength="50"
                  placeholder="请输入NFC"
                  show-word-limit
                  style="width: 230px;"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="额定电流" prop="ratedCurrent" >
              <el-input 
              v-model="form.ratedCurrent" 
              placeholder="请输入额定电流" 
              style="width: 230px;" 
              :disabled="disabledFlag"
              oninput="value=value.replace(/[^\d]/g,'')"
              show-word-limit
              >
               <template #append>
                  <span>A</span>
               </template>
               </el-input>
            </el-form-item>
          </el-col>
            <el-col :span="12"> 
            <el-form-item label="总出行次数" prop="totalTripNum" label-width="100px">
              <el-input
               v-model="form.totalTripNum" 
               placeholder="" 
               style="width: 230px;" 
               :disabled="true"
               oninput="value=value.replace(/[^\d]/g,'')"
               show-word-limit
               >
               </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12"> 
            <el-form-item label="额定电压" prop="nominalVoltage">
              <el-input
                  v-model="form.nominalVoltage"
                  :disabled="disabledFlag"
                  oninput="value=value.replace(/[^\d]/g,'')"
                  placeholder="请输入额定电压"
                  show-word-limit
                  style="width: 230px;"
              >
                <template #append>
                  <span>V</span>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总出行里程" prop="totalTripMile" label-width="100px">
              <el-input 
              v-model="form.totalTripMile" 
              placeholder="" 
              style="width: 230px;" 
              :disabled="true"
              oninput="value=value.replace(/[^\d]/g,'')"
              show-word-limit
              >
               <template #append>
                  <span>km</span>
               </template>
               </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12"> 
            <el-form-item label="制造年月" prop="createYm">
                <el-date-picker
                  v-model="form.createYm" 
                  format="YYYY-MM"
                  value-format="YYYY-MM"
                  clearable
                  :disabled="disabledFlag"
                  :disabledDate="disabledFutureMonth"
                  type="month"
                  placeholder="请选择制造年月"
                />
            </el-form-item>
          </el-col>
          <el-col :span="12">
           <el-form-item style="" label="车辆图片"  prop="carPic" label-width="100px">
              <image-upload
                  v-model="form.carPic"
                  :class="
                {
                  'no-delete-button': disabledFlag,
                  'hide-tip': disabledFlag
                }"
                  :limit="1"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div v-if="disabledFlag == false" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 绑定车辆与车主关系 -->
    <el-dialog v-model="openLock" :title="titleLock" append-to-body width="25%">
      <el-form ref="infoLockRef" :model="formLock" :rules="rulesLock" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="车辆名称" prop="carName">
              <el-input
                  v-model="formLock.carName"
                  :disabled="true"
                  style="width: 59%;"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户信息" label-width="80px" prop="userId">
              <el-select v-model="formLock.userId" clearable placeholder="请选择绑定用户">
                <el-option
                    v-for="item in targetCollection"
                    :key="item.value"
                    :disabled="item.disabled"
                    :label="item.label"
                    :value="item.value"
                >
                  <span style="float: left">{{ item.label }}</span>
                  <span
                      style="
                      float: right;
                      color: var(--el-text-color-secondary);
                      font-size: 13px;
                    "
                  >
                    {{ item.value }}
                  </span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitLockForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script name="Info" setup>
import {addInfo, delInfo, getInfo, listInfo, listUser, lockUserId, unLockUserId, updateInfo} from "@/api/carInfo/info";

const router = useRouter();
const {proxy} = getCurrentInstance();

const infoList = ref([]);
const open = ref(false);
const openLock = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const titleLock = ref("");
const disabledFlag = ref(false);
var checkNum = (rule, value, callback) => {
  if (value < 1 || value > 10000) {
    callback(new Error('必须1~10000的整数'));
  } else {
    callback();
  }
};
const targetCollection = ref([]);
const data = reactive({
  form: {},// 新增 保存表单
  formLock: {},// 绑定车辆 表单
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    carName: null,
    brand: null,
    carVin: null
  },
  rulesLock: {
    userId: [
      {
        required: true,
        message: "用户必须选择",
        trigger: "change"
      },
    ],
  },
  rules: {
    carName: [
      {
        required: true,
        message: "车辆名称不能为空",
        trigger: "change"
      },
      {
        whitespace: true,
        message: '不可以只存在空格',
        trigger: 'blur'
      },
    ],
    createYm: [
      { 
        required: true, 
        message: "制造年月不能为空", 
        trigger: "change" 
      },
    ],
    brand: [
      {
        required: true,
        message: "车辆品牌不能为空",
        trigger: "change"
      },
      {
        whitespace: true,
        message: '不可以只存在空格',
        trigger: 'blur'
      },
    ],
    carVin: [
      {
        required: true,
        message: "车架号不能为空",
        trigger: "change"
      },
      {
        whitespace: true,
        message: '不可以只存在空格',
        trigger: 'blur'
      },
    ],
    horsePower: [
      {
        required: true,
        message: "马力值不能为空",
        trigger: "change"
      },
      {
        whitespace: true,
        message: '不可以只存在空格',
        trigger: 'blur'
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    maxPower: [
      {
        required: true,
        message: "最大功率不能为空",
        trigger: "change"
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    maxTorque: [
      {
        required: true,
        message: "最大扭矩不能为空",
        trigger: "change"
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    carSize: [
      {
        required: true,
        message: "车辆长宽高不能为空",
        trigger: "change"
      },
      {
        whitespace: true,
        message: '不可以只存在空格',
        trigger: 'blur'
      },
    ],
    batteryType:[
      {
        required: true,
        message: "电池类型不能为空",
        trigger: "change"
      },
      {
        whitespace: true,
        message: '不可以只存在空格',
        trigger: 'blur'
      },
    ],
    wheelBase: [
      {
        required: true,
        message: "车辆轴距不能为空",
        trigger: "change"
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    frontWheelBase: [
      {
        required: true,
        message: "车辆前轮距不能为空",
        trigger: "change"
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    backWheelBase: [
      {
        required: true,
        message: "车辆后轮距不能为空",
        trigger: "change"
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    enduranceMileage: [
      {
        required: true,
        message: "续航里程不能为空",
        trigger: "change"
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    kmh: [
      {
        required: true,
        message: "百公里耗电不能为空",
        trigger: "change"
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    batteryCode: [
      {
        required: true,
        message: "电池编码不能为空",
        trigger: "change"
      },
      {
        whitespace: true,
        message: '不可以只存在空格',
        trigger: 'blur'
      },
    ],
    batteryPower: [
      {
        required: true,
        message: "电池电量不能为空",
        trigger: "change"
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    workingTemperature: [
      {
        required: true,
        message: "工作温度不能为空",
        trigger: "change"
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    batteryCapacity: [
      {
        required: true,
        message: "电池容量不能为空",
        trigger: "change"
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    nominalVoltage: [
      {
        required: true,
        message: "额定电压不能为空",
        trigger: "change"
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    nfc: [
      {
        required: true,
        message: "NFC数据不能为空",
        trigger: "change"
      },
      {
        whitespace: true,
        message: '不可以只存在空格',
        trigger: 'blur'
      },
    ],
    ratedCurrent: [
      {
        required: true,
        message: "额定电流不能为空",
        trigger: "change"
      },
      {validator: checkNum, trigger: 'blur'},
    ],
    carPic: [
      {
        required: true,
        message: "车辆图片不能为空",
        trigger: "change"
      },
    ],
  }
});

const {queryParams, form, formLock, rules, rulesLock} = toRefs(data);

/** 查询车辆管理信息列表 */
function getList() {
  loading.value = true;
  listInfo(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  disabledFlag.value = false;
  openLock.value = false;
  targetCollection.value = [];
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    carName: null,
    brand: null,
    carPic: null,
    carVin: null,
    horsePower: null,
    maxPower: null,
    maxTorque: null,
    carSize: null,
    wheelBase: null,
    frontWheelBase: null,
    backWheelBase: null,
    batteryPower: null,
    enduranceMileage: null,
    kmh: null,
    batteryCode: null,
    batteryType: null,
    batteryCapacity: null,
    nominalVoltage: null,
    ratedCurrent: null,
    workingTemperature: null
  };
  proxy.resetForm("infoRef");
  formLock.value = {
    id: null,
    carName: null,
    userId: null
  };
  proxy.resetForm("infoLockRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 禁用未来月份
function disabledFutureMonth(date) {
  const now = new Date();
  return date && date.valueOf() > now.setMonth(now.getMonth());
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加车辆信息";
  disabledFlag.value = false;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  disabledFlag.value = false;
  const id = row.id
  getInfo(id).then(response => {
    form.value = response.data;
    // 制造日期单独赋值
    // const date = new Date(form.value.createYm);
    // const year = date.getFullYear();
    // let month = date.getMonth() + 1; // 月份是从0开始的
    // month = month < 10 ? '0' + month : month; 
    // form.value.createYm = year + "-" + month
    open.value = true;
    title.value = "修改车辆信息";
  });
}

/** 查看按钮操作 */
function handleView(row) {
  reset();
  disabledFlag.value = true;
  const id = row.id
  getInfo(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "查看车辆信息";
  });
}

/** 控制车辆按钮操作 */
function controllCar(row) {
  router.push("/carInfo/car_control/car/" + row.id);
}

/** 提交按钮 */
function submitForm() {
  if (form.value.carPic == null) {
    proxy.$modal.msgError("汽车图片不能为空");
    return
  }
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).catch(() => {
        });
      } else {
        addInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).catch(() => {
        });
      }
    }
  });
}

/** 绑定车俩提交按钮 */
function submitLockForm() {
  proxy.$refs["infoLockRef"].validate(valid => {
    if (valid) {
      lockUserId(formLock.value).then(response => {
        getList();
        reset();
        targetCollection.value = []
        proxy.$modal.msgSuccess("绑定成功")
        openLock.value = false;
      }).catch(() => {
      });
    }
  });
}

/** 解绑操作 */
function unLockUser(row) {
  const id = row.id
  proxy.$modal.confirm('是否确认将该汽车与用户关系解绑？').then(function () {
    return unLockUserId(id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("解绑成功")
  }).catch(() => {
  });
}

/** 绑定操作 */
function lockUser(row) {
  const id = row.id
  openLock.value = true;
  titleLock.value = "绑定车辆用户";
  formLock.value.carName = row.carName
  formLock.value.id = id;
  listUser().then(response => {
    response.data.forEach(item => {
      // 对每个元素进行处理，这里我们直接将其加倍
      let processedItem = {};
      processedItem.value = item.userId;
      processedItem.label = item.nickName;
      // 当前用户绑定了车俩，不能重复绑定
      if (item.usedField == '1') {
        processedItem.disabled = true;
      }
      // 将处理后的元素添加到目标集合中
      targetCollection.value.push(processedItem);
    });
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const ida = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除该车辆信息？').then(function () {
    return delInfo(ida);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

getList();
</script>
<style scoped>
/* 隐藏删除按钮 */
.no-delete-button ::v-deep .el-upload-list__item-actions .el-upload-list__item-delete {
  display: none !important;
}

/* 隐藏提示语 */
.hide-tip ::v-deep .el-upload__tip {
  display: none !important;
}
</style>
